home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / QuickDraw / CollectPictColors / CollectPictColors.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  5.1 KB  |  210 lines  |  [TEXT/KAHL]

  1. /****************************************************************************/
  2. /*                                                                            */
  3. /*    Application:    CollectPictColors                                        */
  4. /*                                                                            */
  5. /*    Description:    This application uses the Picture Utilities package        */
  6. /*                    and Jon Zap's KnowsPict application to demonstrate        */
  7. /*                    two methods of collecting colors used by Pict            */
  8. /*                    resources.  In this program, you'll see different        */
  9. /*                    results for each method.  With the Pict Util package,    */
  10. /*                    the routine, GetPictInfo, returns a colortable with        */
  11. /*                    the number of colors requested, but only the colors        */
  12. /*                    used in the picture or it's pixmap(s) image data are    */
  13. /*                    stored in the requested colors.  As for the                */
  14. /*                    remaining requested colors not used by the picture        */
  15. /*                    but stored in the picture's pixmap(s) colortable,        */
  16. /*                    they are set to black.  In Jon's application this        */
  17. /*                    doesn't occur.  All the colors used by the picture,        */
  18. /*                    including those stored in the picture's pixmap(s)        */
  19. /*                    colortable are returned.  To use Jon's routines,        */
  20. /*                    simply call CollectColors with the appropriate            */
  21. /*                    parameters.                                                */
  22. /*                                                                            */
  23. /*    Files:            CollectPictColors.π                                        */
  24. /*                    CollectPictColors.c                                        */
  25. /*                    CollectPictColors.π.rsrc                                */
  26. /*                    CLUTBuilder.c    (written by Jon Zap)                    */
  27. /*                                                                            */
  28. /*    Programmer:        Edgar Lee                                                */
  29. /*    Organization:    Apple Computer, Inc.                                    */
  30. /*    Department:        Developer Technical Support, DTS                        */
  31. /*    Language:        C (Think C version 5.0.1)                                */
  32. /*    Date Created:    02-20-92                                                */
  33. /*                                                                            */
  34. /****************************************************************************/
  35.  
  36. /* Constant Declarations */
  37.  
  38. #define    WWIDTH        470
  39. #define    WHEIGHT        330
  40.  
  41. #define WLEFT        (((screenBits.bounds.right - screenBits.bounds.left) - WWIDTH) / 2)
  42. #define WTOP        (((screenBits.bounds.bottom - screenBits.bounds.top) - WHEIGHT) / 2)
  43.  
  44. void initMac();
  45. void createWindow();
  46. void drawPictureColors();
  47. void drawColors();
  48. void doEventLoop();
  49.  
  50. main()
  51. {
  52.     initMac();
  53.     
  54.     createWindow();
  55.     
  56.     doEventLoop();
  57. }
  58.  
  59. void initMac()
  60. {
  61.     MaxApplZone();
  62.  
  63.     InitGraf( &thePort );
  64.     InitFonts();
  65.     InitWindows();
  66.     InitMenus();
  67.     TEInit();
  68.     InitDialogs( nil );
  69.     InitCursor();
  70.     FlushEvents( 0, everyEvent );
  71. }
  72.  
  73. void createWindow()
  74. {
  75.     Rect        rect;
  76.     WindowPtr    window;
  77.     
  78.     SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
  79.     window = NewCWindow( 0L, &rect, "\pCollect Pict Colors", true, documentProc,
  80.                             (WindowPtr)-1L, true, 0L );                        
  81.     SetPort( window );
  82.     
  83.     TextFont( geneva );
  84.     TextSize( 9 );
  85.     TextMode( srcCopy );
  86. }
  87.  
  88. void drawPictureColors()
  89. {
  90.     Rect        rect;
  91.     PicHandle    pict;
  92.     CTabHandle    ctable;
  93.     short        depth, directFlag;
  94.     PictInfo     thePictInfo;
  95.     CTabHandle    CollectColors();
  96.     
  97.     /* Load the pict resource. */
  98.     pict = (PicHandle)GetResource( 'PICT', 128 );
  99.     
  100.     /* Make sure it's not purgeable. */
  101.     HNoPurge( pict );
  102.     
  103.     /* Set a black background. */
  104.     SetRect( &rect, 0, 0, WWIDTH, WHEIGHT );
  105.     ForeColor( blackColor );
  106.     PaintRect( &rect );
  107.     
  108.     /* See what CollectColors returns. */
  109.     ctable = CollectColors( pict, &depth, &directFlag );
  110.     drawColors( ctable, 20, "\pCOLLECTCOLORS Colortable" );
  111.     
  112.     /* Draw the picture. */
  113.     rect = (**pict).picFrame;
  114.     OffsetRect( &rect, -rect.left + 160, -rect.top + 70 );
  115.     DrawPicture( pict, &rect );
  116.     
  117.     /* Frame the picture. */
  118.     InsetRect( &rect, -2, -2 );
  119.     FrameRect( &rect );
  120.     
  121.     /* Now see what GetPictInfo returns. */
  122.     GetPictInfo( pict, &thePictInfo, returnColorTable, 256, medianMethod, 0 );
  123.     drawColors( thePictInfo.theColorTable, rect.right + 15, "\pGETPICTINFO Colortable" );
  124.  
  125.     /* Release the resource. */
  126.     ReleaseResource( pict );
  127.     
  128.     /* Release the colortable. */
  129.     DisposCTable( ctable );
  130. }
  131.  
  132. void drawColors( ctable, offset, string )
  133. CTabHandle    ctable;
  134. int            offset;
  135. Str255        string;
  136. {
  137.     int            i;
  138.     int            col, row;
  139.     Rect        rect;
  140.  
  141.     ForeColor( whiteColor );
  142.     BackColor( blackColor );
  143.         
  144.     MoveTo( offset, 20 );
  145.     DrawString( string );
  146.     
  147.     if (ctable != nil)
  148.     {
  149.         for (i = 0; i < 256; i++)
  150.         {
  151.             col = offset + ((i % 8) * 16);
  152.             row = 30 + ((i / 8) * 9);
  153.             
  154.             SetRect( &rect, col, row, col + 10, row + 3 );
  155.             RGBForeColor( &(**ctable).ctTable[i].rgb );
  156.             PaintRect( &rect ); 
  157.             
  158.             ForeColor( whiteColor );
  159.             InsetRect( &rect, -2, -2 );
  160.             FrameRect( &rect );
  161.         }
  162.     }
  163.     else
  164.     {
  165.         MoveTo( 12, offset );
  166.         DrawString( "\p-Sorry could not load PICT resource." );
  167.     }
  168. }
  169.  
  170. void doEventLoop()
  171. {
  172.     EventRecord event;
  173.     WindowPtr   window;
  174.     short       clickArea;
  175.     Rect        screenRect;
  176.  
  177.     for (;;)
  178.     {
  179.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  180.         {
  181.             if (event.what == mouseDown)
  182.             {
  183.                 clickArea = FindWindow( event.where, &window );
  184.                 
  185.                 if (clickArea == inDrag)
  186.                 {
  187.                     screenRect = (**GetGrayRgn()).rgnBBox;
  188.                     DragWindow( window, event.where, &screenRect );
  189.                 }
  190.                 else if (clickArea == inContent)
  191.                 {
  192.                     if (window != FrontWindow())
  193.                         SelectWindow( window );
  194.                 }
  195.                 else if (clickArea == inGoAway)
  196.                     if (TrackGoAway( window, event.where ))
  197.                         return;
  198.             }
  199.             else if (event.what == updateEvt)
  200.             {
  201.                 window = (WindowPtr)event.message;    
  202.                 SetPort( window );
  203.                 
  204.                 BeginUpdate( window );
  205.                 drawPictureColors();
  206.                 EndUpdate( window );
  207.             }
  208.         }
  209.     }
  210. }